package com.feng.util;

import java.util.List;

import com.lenovo.lps.log.Log;
/**
 * 
 * @author liubingzhao
 * @detail Utils for pages
 *
 */
public class Page {
	private static final Log log = Log.getInstance(Page.class);
	public static boolean ALL = true;
	public static boolean SEGMENT = false;
	public enum dataLoadMode {ALL,SEGMENT};
	private int pageSize = 10;
	private int allCount = 0;
	private int currentPage = 1;
	private int pageCount = 0;
	private int begin = 0;
	private int end = 0;
	private boolean dataLoadMode = ALL;
	private List<?> dataList;
	@SuppressWarnings("unused")
	private Page() {
	}
	
	/**
	 * 全部加载数据分页
	 * @param pageSize
	 * @param currentPage
	 * @param dataListe
	 */
	public Page(int pageSize, int currentPage, List<?> dataList) {
		super();
		this.pageSize = pageSize;
		this.currentPage = (currentPage < 1) ? 1 : currentPage;
		this.dataLoadMode = ALL;
		
		this.allCount = dataList!=null ? dataList.size() : 0;
		if(this.allCount > 0){
			pageCount = (this.allCount + pageSize - 1)/pageSize;
			if(this.currentPage > pageCount && pageCount > 0){
				this.currentPage = pageCount;
			}
				
			this.begin = this.pageSize*(this.currentPage-1)+1;
			this.end = this.pageSize*this.currentPage;
			
			if(end > this.allCount)
				end = this.allCount;
			log.debug("PageInfo:::  dataLoadMode is: ALL "+this.dataLoadMode);
			log.debug("PageInfo:::  allCount is:"+this.allCount);
			log.debug("PageInfo:::  currentPage is:"+this.currentPage);
			log.debug("PageInfo:::  pageSize is:"+this.pageSize);
			log.debug("PageInfo:::  begin index::"+begin+" : end index::"+end );
			if(this.dataLoadMode == ALL)
			this.dataList = dataList.subList(begin-1,end);
		}
	}
	
	/**
	 * 部分加载数据分页
	 * @param allCount
	 * @param pageSize
	 * @param currentPage
	 * @param dataList
	 */
	public Page(int allCount,int pageSize, int currentPage,List<?> dataList) {
		super();
		this.pageSize = pageSize;
		this.currentPage = (currentPage < 1) ? 1 : currentPage;
		this.dataLoadMode = SEGMENT;
		this.allCount = dataList!=null ? allCount : 0;
		if(this.allCount > 0){
			pageCount = (this.allCount + pageSize - 1)/pageSize;
			if(this.currentPage > pageCount && pageCount > 0){
				this.currentPage = pageCount;
			}
			this.begin = this.pageSize*(this.currentPage-1)+1;
			this.end = this.pageSize*this.currentPage;
			
			if(end > this.allCount)
				end = this.allCount;
			log.debug("PageInfo:::  dataLoadMode is: SEGMENT "+this.dataLoadMode);
			log.debug("PageInfo:::  allCount is:"+this.allCount);
			log.debug("PageInfo:::  currentPage is:"+this.currentPage);
			log.debug("PageInfo:::  pageSize is:"+this.pageSize);
			log.debug("PageInfo:::  begin index::"+begin+" : end index::"+end );
			this.dataList = dataList;
		}
	}
	
	/**
	 * 部分加载数据分页
	 * @param pageSize page size
	 * @param currentPage current num
	 */
	public Page(int pageSize, int currentPage) {
		super();
		this.pageSize = pageSize;
		this.currentPage = (currentPage < 1) ? 1 : currentPage;
		this.begin = this.pageSize*(this.currentPage-1)+1;
		this.end = this.pageSize*this.currentPage;
		this.dataLoadMode = SEGMENT;
	}
	public int getPageSize() {
		return pageSize;
	}
	public int getAllCount() {
		return allCount;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public int getPageCount() {
		return pageCount;
	}
	public List<?> getDataList() {
		return dataList;
	}
	public int getBegin() {
		return begin;
	}
	public int getEnd() {
		return end;
	}
	public void setAllCount(int allCount) {
		this.allCount = allCount;
		if(this.allCount > 0){
			pageCount = (this.allCount + pageSize - 1)/pageSize;
			if(this.currentPage > pageCount && pageCount > 0){
				this.currentPage = pageCount;
			}
			this.begin = this.pageSize*(this.currentPage-1)+1;
			this.end = this.pageSize*this.currentPage;
			if(end > this.allCount)
				end = this.allCount;
		}
		log.debug(">PageInfo:::  dataLoadMode is: SEGMENT "+this.dataLoadMode);
		log.debug(">PageInfo:::  allCount is:"+this.allCount);
		log.debug(">PageInfo:::  currentPage is:"+this.currentPage);
		log.debug(">PageInfo:::  pageSize is:"+this.pageSize);
		log.debug(">PageInfo:::  begin index::"+begin+" : end index::"+end );
	}
	public void setDataList(List<?> dataList) {
		this.dataList = dataList;
	}
}
